package com.wpp.lagouserviceuser.controller;

import com.wpp.core.result.ResultDTO;
import com.wpp.lagouserviceuser.param.UserParam;
import com.wpp.lagouserviceuser.pojo.UserPO;
import com.wpp.lagouserviceuser.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author wpp25
 * @date 2021/5/13 21:52
 * @description：
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserService userService;

    @Value("${server.port}")
    private Integer port;

    @PostMapping("/register")
    public ResultDTO register(@RequestBody UserParam user) {
        return new ResultDTO(userService.register(user),String.valueOf(port),user);
    }

    @GetMapping("/registerGet")
    public ResultDTO registerGet(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            @RequestParam("email") String email,
            @RequestParam("code") String code

    ) {
        UserParam userParam = new UserParam();
        userParam.setUserName(userName);
        userParam.setPassword(password);
        userParam.setCode(code);
        userParam.setEmail(email);
        return new ResultDTO(userService.register(userParam),String.valueOf("post: " +port),userParam);
    }

    @GetMapping("/login")
    public ResultDTO login(@RequestParam("user_name") String userName, @RequestParam("password")String password) {

        UserPO userPO = userService.queryByUserName(userName);

        return new ResultDTO(Objects.nonNull(userPO) && password.equals(userPO.getPassword()),String.valueOf("post: " +port),userPO);
    }


}
